/*
 * =====================================================================================
 *
 *       Filename:  dup2.c
 *
 *    Description:  demo of use dup and pipe
 *
 *        Version:  1.0
 *        Created:  05/04/2012 02:27:29 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Zhiyi Wu (Z.W), zhiyiw@uoregon.edu
 *        Company:  University of Oregon
 *
 * =====================================================================================
 */

#include <stdio.h>  
#include <stdlib.h>  
#include <unistd.h>


int main()  
{  
	int pfds[2];  
	if(pipe(pfds)==0)  
	{  
		if(fork()==0)  
		{  
			close(1);  
			dup2(pfds[1],1);  
			close(pfds[0]);  
//			printf("%d\n",pfds[0]);
			execlp( "ls","ls","-l",NULL );  

		}  
		else  
		{  
			close(0);  
			dup2(pfds[0],0);  
			close(pfds[1 ]); 
	//		printf("%d\n",pfds[0]);
			execlp( "wc","wc","-l",NULL );  


		}  
	}  
	return 0;  

}  
